
/**
 * @author 郭敖
 * @codeName 工具库
 * @description 工具库
 * @createTime 2024-08-19
 */
class GAUtil {

    /**
     * 根据姓名返回员工ID 需要进行判空操作
     */
    static String getUserIdByUserName(String username){
        def ret = Fx.object.find("PersonnelObj", //查询客户对象
                FQLAttribute.builder()
                        .columns(["_id", "name","user_id"]) //返回的数据id
                        .queryTemplate(QueryTemplate.AND(["name": QueryOperator.EQ(username)]))
                        .build(),
                SelectAttribute.builder().build()).result() as QueryResult
        List resList = ret.dataList as List
        // 查询不到userId时的兜底方案
        if(resList.size() == 0){
            return ""
        }
        Map map = ret.dataList[0]
        return map.user_id as String
    }

    /**
     * 发送CRM通知
     */
    static Void sendCRMMessage(List idList,String objectAPIName,String ObjectId,String title,String MessageContent){
        List receiverIds = idList
        Notice objectNotify = Notice.objectNotice(objectAPIName,ObjectId)
        def (Boolean error, String data, String errorMessage) = Fx.message.sendNotice(title,MessageContent,receiverIds, objectNotify)
        if (error) {
            log.info(errorMessage)
        } else {
            log.info(data)
        }

    }

    // 从Map中获取值（带默认值）的方法
    static def getValueWithDefault = {
        Map data,Closure<Map> closure ->
            def value = closure(data)
            switch(value) {
                case value instanceof String:
                    String res = value  as String
                    return res == null ? "" : res
                    break;
                case value instanceof Integer:
                    Integer res = value as Integer
                    return res == null ? 0 : res
                    break;
                case value instanceof Double:
                    Double res = value as Double
                    return res == null ? 0 : res
                    break;
                case value instanceof Float:
                    Float res = value as Float
                    return res == null ? 0 : res
                    break;
                case value instanceof Boolean:
                    Boolean res = value as Boolean
                    return res == null ? false : res
                    break;
                case value instanceof List:
                    List res = value as List
                    return res == null? [] : res
                    break;
                case value instanceof Map:
                    Map res = value as Map
                    return res == null ? [:] : res
                    break;
                default:
                    return value == null ? null: value
            }
    }
    private static class ValueUtil {
        private Map data;

        public void setData(Map map){
            data = map
        }
        def getValue(Map data,Closure closure){
            def value = closure(data)
            switch(value) {
                case value instanceof String:
                    String res = value  as String
                    return res == null ? "" : res
                    break;
                case value instanceof Integer:
                    Integer res = value as Integer
                    return res == null ? 0 : res
                    break;
                case value instanceof Double:
                    Double res = value as Double
                    return res == null ? 0 : res
                    break;
                case value instanceof Float:
                    Float res = value as Float
                    return res == null ? 0 : res
                    break;
                case value instanceof Boolean:
                    Boolean res = value as Boolean
                    return res == null ? false : res
                    break;
                case value instanceof List:
                    List res = value as List
                    return res == null? [] : res
                    break;
                case value instanceof Map:
                    Map res = value as Map
                    return res == null ? [:] : res
                    break;
                default:
                    return value == null ? null: value
            }
        }
        def getValueWithDefault(Closure closure){
            return this.getValue(data,closure)
        }
    }

    static ValueUtil getValueUtil(){
        return new ValueUtil();
    }

    def getOptionalValue(String s){
        String optional = Optional.ofNullable(s).orElse("")
    }
}



